﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using COC.Graphics;
using COC.Sound;

namespace COC.GameLogic
{
	public class PlayerActor : AnimActor
	{
		#region Properties

		readonly int m_MaxHp;
		int m_Hp;
		int m_Attack;
		string m_currentAction;
		float m_Speed;
		double m_CoolDown = -0.01;
		WeaponActor m_weapon;

		#endregion

		public PlayerActor(string name, string modelName,
			int maxHp, int attack, float speed)
			: base(name, modelName)
		{
			m_ActorCharacter.AnimController.StartClip(
				m_SkModel.AnimationClips["Idle"]);
			m_currentAction = "Idle";
			m_Hp = m_MaxHp = maxHp;
			m_Attack = attack;
			m_Speed = speed;
		}

		public void SetWeapon(string weaponName)
		{
			m_weapon = new WeaponActor(m_ModelName + "'sGun",
				weaponName, this, 0, 10, 0);
		}

		public bool Update(GameTime gameTime)
		{
			if (m_Diposed)
			{
				return false;
			}

			float y = TerrainManager.Singleton.SampleHeightMap(m_Position);
			m_Position.Y = (y - m_Culling.BoundingBox.Min.Y);

			Matrix ltow = Matrix.CreateFromYawPitchRoll(m_Rotation.Y, m_Rotation.X, m_Rotation.Z)
				* Matrix.CreateTranslation(m_Position);

			m_ActorCharacter.AnimController.Update(gameTime.ElapsedGameTime, ltow);
			m_LocalToWorldMat = ltow;

			Vector3 direction = new Vector3(0, 0, 1);
			direction = Vector3.TransformNormal(direction, m_LocalToWorldMat);

			switch (m_currentAction)
			{
				case "Bite1":
					if (m_CoolDown < 0)
					{
						m_CoolDown = 500;
						//Attack();
					}
					else
					{
						m_CoolDown -= gameTime.ElapsedGameTime.TotalMilliseconds;
					}
					break;
				case "Shoot":
					if (m_CoolDown < 0)
					{
						m_CoolDown = 230;
						m_weapon.Attack(m_Attack);
					}
					else
					{
						m_CoolDown -= gameTime.ElapsedGameTime.TotalMilliseconds;
					}
					break;
			}

			if (m_weapon != null)
				m_weapon.Update(gameTime);
			
			return true;
		}

		public void DoAction(string action)
		{
			if (m_currentAction == action)
			{
				return;
			}

			if (m_SkModel.AnimationClips.ContainsKey(action))
			{
				m_ActorCharacter.AnimController.CrossFade(
					m_SkModel.AnimationClips[action], TimeSpan.FromMilliseconds(50));
				m_currentAction = action;
			}
			else
			{
				m_ActorCharacter.AnimController.CrossFade(
					m_SkModel.AnimationClips["Idle"], TimeSpan.FromMilliseconds(50));
				m_currentAction = "Idle";
			}
		}

		public new void Dispose()
		{
			if (m_weapon != null)
				m_weapon.Dispose();
			base.Dispose();
		}

		public void OnShoot(int damage)
		{
			m_Hp -= damage;
			if(m_Hp <=0)
			{
				Die();
			}
		}

		public void Die()
		{
			SoundManager.PlaySound("Die");
			CoCGame.Singleton.CurrentGameWorld.m_Animation.RemoveInstance(this);
			CoCGame.Singleton.CurrentGameWorld.RemoveMob(this.Name);
		}
	}
}